package com.raidpoints.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Button;

public class ChangePassBox extends DialogBox {
	RaidPoints rp;
	public ChangePassBox(RaidPoints mrp) {
		rp = mrp;
		setHTML("Change password");
		
		LayoutPanel layoutPanel = new LayoutPanel();
		setWidget(layoutPanel);
		layoutPanel.setSize("273px", "163px");
		
		Label lblNewLabel = new Label("Password:");
		layoutPanel.add(lblNewLabel);
		layoutPanel.setWidgetLeftWidth(lblNewLabel, 12.0, Unit.PX, 71.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblNewLabel, 35.0, Unit.PX, 16.0, Unit.PX);
		
		final PasswordTextBox passBox = new PasswordTextBox();
		layoutPanel.add(passBox);
		layoutPanel.setWidgetLeftWidth(passBox, 119.0, Unit.PX, 134.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(passBox, 23.0, Unit.PX, 32.0, Unit.PX);
		
		Label lblReenterPass = new Label("Re-enter Pass:");
		layoutPanel.add(lblReenterPass);
		layoutPanel.setWidgetLeftWidth(lblReenterPass, 12.0, Unit.PX, 117.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(lblReenterPass, 75.0, Unit.PX, 16.0, Unit.PX);
		
		final PasswordTextBox repassBox = new PasswordTextBox();
		layoutPanel.add(repassBox);
		layoutPanel.setWidgetLeftWidth(repassBox, 119.0, Unit.PX, 134.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(repassBox, 61.0, Unit.PX, 32.0, Unit.PX);
		
		Button createButton = new Button("Change");
		layoutPanel.add(createButton);
		layoutPanel.setWidgetLeftWidth(createButton, 33.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(createButton, 119.0, Unit.PX, 28.0, Unit.PX);
		
		Button cancelButton = new Button("Cancel");
		layoutPanel.add(cancelButton);
		layoutPanel.setWidgetLeftWidth(cancelButton, 140.0, Unit.PX, 81.0, Unit.PX);
		layoutPanel.setWidgetTopHeight(cancelButton, 119.0, Unit.PX, 28.0, Unit.PX);
		
		cancelButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				hide();				
			}
			
		});
		
		final AsyncCallback<String> newUserCallback = new AsyncCallback<String>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Password Change failed");				
			}

			@Override
			public void onSuccess(String result) {
				hide();
			}
			
		};
		
		createButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(!passBox.getText().equals(repassBox.getText())){
					Window.alert("Passwords do not match");
					return;
				}
			
				else{
					rp.rpsvc.ChangePass(passBox.getText(), newUserCallback);
				}
			}
			
		});
	}
}
